package com.itheima.utils;

import javax.xml.bind.annotation.XmlType;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
* 日期时间工具类
* */
public class DateUtil {
    private static final String DEFAULT_PATTERN="yyyy-MM-dd";
    private DateUtil(){}
    //将时间转换成字符串形式
    public static String date2Str(Date date, String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(date);
        return str;
    }
    public static String date2Str(Date date){
        return date2Str(date,DEFAULT_PATTERN);
    }
    //字符串转换时间
    public static Date str2Date(String str,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }


    public static Date str2Date(String str){
        return str2Date(str,DEFAULT_PATTERN);
    }
}
